package com.kuang.service;

import com.kuang.dao.BaseDao;
import com.kuang.dao.role.RoleDao;
import com.kuang.dao.role.RoleDaoImpl;
import com.kuang.dao.user.UserDao;
import com.kuang.dao.user.UserDaoImpl;
import com.kuang.pojo.Role;
import com.kuang.pojo.User;
import org.junit.jupiter.api.Test;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

public class UserServiceImpl implements UserService{

    //业务层都会调用dao层,所以我们要引用Dao层;
    private UserDao userDao;
    //获得一个RoleDao对象
    private RoleDao roleDao;
    public UserServiceImpl(){//实例化 以后会交给容器

        this.userDao = new UserDaoImpl();
        this.roleDao = new RoleDaoImpl();

    }
    //用户登录
    public User login(String userCode, String password) {
        Connection connection = null;
        User user = null;
        connection = BaseDao.getConnection();

        //通过业务层调用对应的具体数据库操作
        user = userDao.getLoginUser(connection,userCode);

        BaseDao.close(connection,null,null);
        return user;
    }

    //根据用户ID修改密码
    public boolean updatePwd( int id, String password) {
        Connection connection = null;
        boolean flag = false;
        //修改密码
        connection = BaseDao.getConnection();
        if(userDao.updatePwd(connection,id,password)>0){
            flag = true;
        }
        BaseDao.close(connection,null,null);
        return flag;
    }

    //查询记录数
    public int getUserCount(String username, int userRole) {
        Connection connection = null;
        int count = 0;
        try {
            connection = BaseDao.getConnection();
            count = userDao.getUserCount(connection,username,userRole);
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            BaseDao.close(connection,null,null);
        }
        return count;
    }

    //获取用户列表
    public List<User> getUserList(String queryUserName, int queryUserRole, int currentPageNo, int pageSize) {
        Connection connection = null;
        List<User> userList = null;

        try {
            connection = BaseDao.getConnection();
            userList = userDao.getUserList(connection, queryUserName, queryUserRole, currentPageNo, pageSize);
        } catch (Exception throwables) {
            throwables.printStackTrace();
        } finally {
            BaseDao.close(connection,null, null);
        }
        return userList;
    }

    //获取角色列表的抽象方法
    public List<Role> getRoleList() {
        List<Role> roleList = null;
        Connection connection = null;
        try {
            connection = BaseDao.getConnection();
            roleList = roleDao.getRoleList(connection);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            BaseDao.close(connection, null, null);
        }
        return roleList;
    }

    //添加用户
    public boolean add(User user) {
        Connection connection = null;
        boolean flag = false;
        try {
            connection = BaseDao.getConnection();
            //关闭自动提交，默认开启jdbc事物
            connection.setAutoCommit(false);
            int addRows = userDao.add(connection, user);
            connection.commit();
            if (addRows > 0) {
                flag = true;
            }
        } catch (Exception throwables) {
            throwables.printStackTrace();
            try {
                connection.rollback();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        } finally {
            BaseDao.close(connection,null, null);
        }
        return flag;
    }

    //删除用户
    public boolean delete(int id) {
        Connection connection = null;
        boolean flag = false;
        connection = BaseDao.getConnection();
        int delete = userDao.delete(connection,id);
        if(delete > 0){
            flag = true;
        }
        BaseDao.close(connection,null,null);
        return flag;
    }

    //修改用户
    public boolean userModify(User user) {
        Connection connection = null;
        boolean flag = false;
        try {
            connection = BaseDao.getConnection();
            //关闭自动提交，默认开启jdbc事物
            connection.setAutoCommit(false);
            int updateRows = userDao.userModify(connection, user);
            connection.commit();
            if (updateRows > 0) {
                flag = true;
            }
        } catch (Exception throwables) {
            throwables.printStackTrace();
            try {
                connection.rollback();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        } finally {
            BaseDao.close(connection,null, null);
        }
        return flag;
    }

    //通过id获取用户
    public User updateById(int id) {
        Connection connection = null;
        connection = BaseDao.getConnection();
        User user = userDao.selectById(connection,id);
        BaseDao.close(connection,null,null);
        return user;
    }


    @Test
    public void test1(){
        System.out.println("Hello,SMBMS!");
        UserServiceImpl userService = new UserServiceImpl();
        User admin = userService.login("liming","123456");
        System.out.println(admin.getUserPassword());
    }
    @Test
    public void test2(){
        Connection connection = BaseDao.getConnection();
        PreparedStatement pspm = null;

        String sql = "update smbms_user set userName='管理员' where userCode='admin'";
        Object []parma = {"admin"};
        try {
            BaseDao.execute(connection,pspm,sql,null);
            System.out.println("修改成功");
            BaseDao.close(connection,pspm,null);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
    @Test
    public void test3(){
        UserServiceImpl userService = new UserServiceImpl();
        int userCount = userService.getUserCount(null,0);
        System.out.println(userCount);
    }
    @Test
    public void test4() {
        UserServiceImpl roleService = new UserServiceImpl();
        List<Role> roleList = roleService.getRoleList();
        for (Role role : roleList) {
            System.out.println(role.getRoleName());
        }
    }
    @Test
    public void test5(){
        UserServiceImpl userService = new UserServiceImpl();
        User user  = userService.updateById(1);
        System.out.println(user.toString());
    }
    @Test
    public void test(){
        String id = "5";
        Integer ById = Integer.parseInt(id);
        System.out.println(ById);
    }
}
